package fr.java_freelance.tryspock.calendar;

import fr.java_freelance.tryspock.data.CalendarDao;
import fr.java_freelance.tryspock.data.CalendarDaoStatic;
import fr.java_freelance.tryspock.domain.SpockInfo;
import fr.java_freelance.tryspock.SpockResource;

import static org.junit.Assert.*;
import spock.lang.*;

class CalendarTest  extends spock.lang.Specification {
  @Shared SpockResource spockResource = new SpockResource(new CalendarDaoStatic())
  
  def "String param should correspond to numeric spockInfoDay"() {
    setup:
    
    expect:
    spockResource.findCalendarByDay(day).day == dayNumeric
    
    where:
    day     | dayNumeric
    "1"  | 1
    "2"  | 2
    "3"  | 3
  }
  
  def "String param should correspond to numeric spockInfoDay - table syntax"() {
    setup:
    
    expect:
    spockResource.findCalendarByDay(day).day == dayNumeric
    
    where:
    day  <<   ["1", "2", "3"]
    dayNumeric << [1, 2, 3]
    
  }
  
  
  def "test comportement"() {
    given:
    def calendarDao = Mock(CalendarDao)
    def spockResource = new SpockResource(calendarDao)
    assert spockResource.calendarDao!=null
    
    when :
    spockResource.findCalendarByDay("1")
    
    then :
    1 * calendarDao
    .getInfosByDay("1")
  }
  
  def "test retour"() {
    setup:
    def calendarDao = Mock(CalendarDao)
    def calendar = new SpockResource(calendarDao)
    
    when :
    def spockInfo = spockResource.findCalendarByDay("1")
    
    then :
    calendarDao
    .getInfosByDay(1) >> new SpockInfo("1");
    spockInfo.day == 1
  }
  
  def "test exception"() {
    setup:
    def calendarDao = Mock(CalendarDao)
    def spockResource = new SpockResource(calendarDao)
    
    when :
    def spock = spockResource.findCalendarByDay("1")
    
    then :
        //Simulation problem divers
    1*calendarDao
    .getInfosByDay("1") >>   {throw new Exception("test")} 
    thrown(Exception)
  }
  
  
}  
